package com.tianwen.management.controller;

import com.tianwen.management.entity.Permission;
import com.tianwen.management.result.Result;
import com.tianwen.management.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @program: personnel-management
 * @description
 * @author: tianwen
 * @create: 2023-05-17 09:05
 **/
@RestController
@RequestMapping("/permission")
public class PermissionController {
    @Autowired
    private PermissionService permissionService;
    @GetMapping("/findAll")
    public Result findAll(){
        List<Permission> permissionList = permissionService.list();
        return Result.ok().data("permissionList",permissionList);
    }
    @GetMapping("/page/{page}/{pageSize}")
    public Result page(@PathVariable Integer page, @PathVariable Integer pageSize, String name){
        List<Permission> permissionList=permissionService.page(page, pageSize, name);
        return Result.ok().data("permissionList",permissionList);
    }
    @PostMapping("/add")
    @PreAuthorize("hasAuthority('权限增删改')")
    public Result add(@RequestBody Permission permission){
        permissionService.save(permission);
        return Result.ok();
    }
    @GetMapping("/delete")
    @PreAuthorize("hasAuthority('权限增删改')")
    public Result delete(Long id){
        if (permissionService.removeAndRoleAndUser(id)) {
            return Result.ok();
        }else{
            return Result.error();
        }
    }
    @PostMapping("/update")
    @PreAuthorize("hasAuthority('权限增删改')")
    public Result update(@RequestBody Permission permission){
        permissionService.save(permission);
        return Result.ok();
    }
    @GetMapping("/findByRoleId")
    public Result findByRoleId(Long roleId){
        List<Permission> permissionList=permissionService.findByRoleId(roleId);
        return Result.ok().data("permissionList",permissionList);
    }
}
